package net.playcabbage.utility;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import net.playcabbage.beans.CheckinBean2;
import net.playcabbage.database.operations.CabbageConnection;

public class CheckinUtility {

	/* Performs a single checkin
	 * returns a single bean.
	 */
	public CheckinBean2 doCheckin(String urlString) {

		CheckinBean2 cb = null;
		try {
			CabbageConnection cc = new CabbageConnection();
			cb = cc.checkin(urlString);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cb;

	}

	/* returns the last 10 checkins as a list of beans.
	 * 
	 */
	public List<CheckinBean2> getLatestCheckins() {
		List<String> l = null;
		try {
			CabbageConnection cc = new CabbageConnection();
			l = cc.getLastCheckins(10);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		List<CheckinBean2> cbList = null;
		if (l != null && l.size() >0) {
			cbList = new ArrayList<CheckinBean2>();
			
			// Assumes that the info for a bean is in sets of 3.
			for (int x = 0; x < l.size(); x+=3 ){
				CheckinBean2 cb = new CheckinBean2(l.get(x),l.get(x+2),l.get(x+1));
				cbList.add(cb);
			}
		}

		return cbList;

	}
	
	public List<CheckinBean2> getLatestCheckinsJSON() {
		List<String> l = null;
		try {
			CabbageConnection cc = new CabbageConnection();
			l = cc.getLastCheckins(10);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		List<CheckinBean2> cbList = null;
		if (l != null && l.size() >0) {
			cbList = new ArrayList<CheckinBean2>();
			
			// Assumes that the info for a bean is in sets of 3.
			for (int x = 0; x < l.size(); x+=3 ){
				CheckinBean2 cb = new CheckinBean2(l.get(x),l.get(x+2),l.get(x+1));
				cbList.add(cb);
			}
		}

		return cbList;

	}
	
	public JSONArray makeJSONArray(ResultSetMetaData rsmd, ResultSet rs) throws SQLException, JSONException {
		JSONArray jArray = new JSONArray();

		while(rs.next()) {
			int numColumns = rsmd.getColumnCount();
			JSONObject obj = new JSONObject();

			for (int i=1; i<numColumns+1; i++) {
				String column_name = rsmd.getColumnName(i);

				if(rsmd.getColumnType(i)==java.sql.Types.ARRAY){
					obj.put(column_name, rs.getArray(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.BIGINT){
					obj.put(column_name, rs.getInt(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.BOOLEAN){
					obj.put(column_name, rs.getBoolean(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.BLOB){
					obj.put(column_name, rs.getBlob(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.DOUBLE){
					obj.put(column_name, rs.getDouble(column_name)); 
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.FLOAT){
					obj.put(column_name, rs.getFloat(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.INTEGER){
					obj.put(column_name, rs.getInt(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.NVARCHAR){
					obj.put(column_name, rs.getNString(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.VARCHAR){
					obj.put(column_name, rs.getString(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.TINYINT){
					obj.put(column_name, rs.getInt(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.SMALLINT){
					obj.put(column_name, rs.getInt(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.DATE){
					obj.put(column_name, rs.getDate(column_name));
				}
				else if(rsmd.getColumnType(i)==java.sql.Types.TIMESTAMP){
					obj.put(column_name, rs.getTimestamp(column_name));   
				}
				else{
					obj.put(column_name, rs.getObject(column_name));
				}
			}

			jArray.put(obj);
		}

		return jArray;
	}



}



//public String getLastCheckinsHTML() throws SQLException {
//	CabbageConnection cc = new CabbageConnection();
//	String string = null;
//
//	List<String> sList = cc.getLastCheckins();
//
//	for (String s : sList) {
//		if (string == null) {
//			string = s + "<br>";
//		} else {
//			string += s + "<br>";
//		}
//		string += "<p id=\"testhidden\">This is a hidden section</p>";
//	}
//
//	return string;
//}
//
//private void getCheckinScriptElement() {
//	String s = "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js\">" + 
//	"</script>" +
//	"<script>" + 
//	"$(document).ready(function(){" +
//	  "$(\"#hide\").click(function(){" +
//	    "$(\"p\").hide();" +
//	  "});" +
//	  "$(\"#show\").click(function(){" +
//	    "$(\"p\").show();" +
//	  "});" +
//	"});" +
//	"</script>";
//}
